<?php

$julib_path='../../../jujube/app/jujud/php/lib';
set_include_path(get_include_path() . PATH_SEPARATOR . $julib_path);
$foxlib_path='../lib/php';
set_include_path(get_include_path() . PATH_SEPARATOR . $foxlib_path);

require_once('logger.php');
require_once('optparse.php');
require_once('cache_client.php');

define('HOST', '127.0.0.1');
define('PORT', 54344);
define('SERVICE', 'CACHE');

$opt_parser = new OptionParser();
$opt_parser->addOption('', '--verbose', 'verbose', 'show verbose information', 'bool');
$opt_parser->addOption('', '--times', 'times', 'times to ping-pong');
$opt_parser->addOption('', '--bytes', 'bytes', 'size of message to ping-pong');
try {
	list($options, $args) = $opt_parser->parse( $argv );
}
catch(Exception $ex) {
	echo 'Exception: ',$ex->getMessage(),"\n";
	exit(2);
}

$verbose = false;
if (isset($options['verbose'])) {
    $verbose = $options['verbose'];
}
if (isset($options['times'])) {
    $times = $options['times'];
}
else {
    $times = 10;
}
if ($times < 1) { 
    $times = 10;
}
if (isset($options['bytes'])) {
    $bytes = $options['bytes'];
}
else {
    $bytes = 0;
}
if ($bytes < 1) {
    $bytes = 0;
} 
$bytes_pad = str_repeat('A', $bytes);

///////////////////////////////////////////////////////////////////////

$server_address = array(HOST, PORT);
$server_addresses = array();
$server_addresses[] = $server_address;

$logger = getLogger();
$cache = new CacheClient(SERVICE, $server_addresses);

$time_start = microtime(true);

// insert test
for ($i=0; $i<$times; $i++) {
    $key = sprintf('key--test--%08d', $i);
    $data = sprintf('data-test-%08d', $i);
    $timeout = 10;
    try {
        $status = $cache->insert($key, $data, $timeout);
    }
    catch(Exception $ex) {
        $logger->error(sprintf('Exception: %s', $ex->getMessage()));
    }
    if (($status != 200) && ($status != 201)) {
        $logger->error(sprintf('Fail to insert (key=%s, data=%s, timeout=%d): status=%d', $key, $data, $timeout, $status));
    }
    elseif ($status == 201) {
        if ($verbose) {
            $logger->error(sprintf('Ignore insertion (key=%s, data=%s, timeout=%d).', $key, $data, $timeout));
        }
    }
}

// lookup test
for ($i=0; $i<$times; $i++) {
    $key = sprintf('key--test--%08d', $i);
    $data = sprintf('data-test-%08d', $i);
    $timeout = 1;
    try {
        list($status, $value) = $cache->lookup($key);
    }
    catch(Exception $ex) {
        $logger->error(sprintf('Exception: %s', $ex->getMessage()));
    }
    if (($status != 200) && ($status != 202)) {
        $logger->error(sprintf('Fail to lookup (key=%s): status=%d', $key, $status));
    }
    elseif ($status == 202) {
        if ($verbose) {
            $logger->error(sprintf('Expired to lookup (key=%s)', $key));
        }
    }
    elseif ($data != $value) {
        $logger->error(sprintf('Fail to lookup (key=%s): value=%s expect=%s', $key, $value, $data));
    }
}

$time_stop = microtime(true);

unset($cache);

$time_last = $time_stop - $time_start;
$tps = sprintf('%.2f', $times / $time_last);
printf('times: %d, time-last: %.2f seconds, tps: %.2f %s', $times, $time_last, $tps, PHP_EOL);
